/*
 *所有关于xj_review_progress类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*xj_review_progressService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using Dapper;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;
using VOL.AppManager.IRepositories;
using VOL.Core.Extensions;
using VOL.Core.ManageUser;
using VOL.Entity.DomainModels;
using VOL.Entity.ResDto;
using VOL.Entity.新文件夹;

namespace VOL.AppManager.Services
{
    public partial class xj_review_progressService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Ixj_review_progressRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public xj_review_progressService(
            Ixj_review_progressRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }


        public PageGridData<xjreviewprogressdto> GetData(xjreviewprogressQuery options)
        {
            DynamicParameters sqlParams = new DynamicParameters();

            string sql = "   select  row_number() OVER () AS idx,  * From ( select  * from xj_review_progress  where 1=1  order by xzqdm  ) t where 1=1 ";
            string sqlcount = "    select  count(1)  from xj_review_progress where 1=1 ";
            if (UserContext.Current.RoleId == 137)
            {
                sql = "    select   row_number() OVER () AS idx,  * from (select *  from dsj_review_progress  where 1=1) t where 1=1  ";
                sqlcount = "    select  count(1)  from dsj_review_progress where 1=1 ";
            }

            if (options.sq_sj != null && options.sq_sj.Count == 2)
            {
                var start_time = Convert.ToDateTime(options.sq_sj[0] + "").AddDays(1).ToString("yyyy/MM/dd") + "";
                var end_time = Convert.ToDateTime(options.sq_sj[1] + "").AddDays(1).ToString("yyyy/MM/dd") + "";
                //sj = sj.AddMinutes(59);
                //sj = sj.AddSeconds(59);
                sqlcount += " and create_time between '" + start_time + " 00:00:00' and '" + end_time + "  23:59:59'  ";
                sql += " and create_time between '" + start_time + " 00:00:00' and '" + end_time + " 23:59:59'  ";

            }
            if (options.sh_sj != null && options.sh_sj.Count == 2)
            {
                var start_time = Convert.ToDateTime(options.sh_sj[0] + "").AddDays(1).ToString("yyyy/MM/dd") + "";
                var end_time = Convert.ToDateTime(options.sh_sj[1] + "").AddDays(1).ToString("yyyy/MM/dd") + "";
                sqlcount += " and sh_time  between '" + start_time + " 00:00:00' and '" + end_time + "  23:59:59'  ";
                sql += " and sh_time  between '" + start_time + " 00:00:00' and '" + end_time + "  23:59:59'  ";
            }
            if (options.shzt != null)
            {
                if (options.shzt == -1)
                {
                    sqlcount += " and shzt ='未审核'  ";
                    sql += " and shzt ='未审核'   ";
                }
                else if (options.shzt == 1)
                {
                    sqlcount += " and shzt ='已通过'  ";
                    sql += " and shzt ='已通过'   ";
                }
                else if (options.shzt == 0)
                {
                    sqlcount += " and shzt ='不通过'  ";
                    sql += " and shzt ='不通过'   ";
                }
                else if (options.shzt == -3)
                {
                    sqlcount += " and shzt ='未提交'  ";
                    sql += " and shzt ='未提交'   ";
                }
            }
            if (!string.IsNullOrEmpty(options.citydm))
            {

                sqlcount += " and citydm='" + options.citydm + "'";
                sql += " and citydm='" + options.citydm + "'";
            }
            sql += "   limit @pagesize offset (@pageindex-1)*@pagesize  ";
            sqlParams.Add("pageindex", options.pageindex);
            sqlParams.Add("pagesize", options.pagesize);
            var datacount = repository.DapperContext.ExecuteScalar(sqlcount, sqlParams).GetInt();
            var data = repository.DapperContext.QueryList<xjreviewprogressdto>(sql, sqlParams).ToList();
            int totalPages = datacount / options.pagesize;
            if (datacount % options.pagesize != 0)
            {
                totalPages++;
            }
            return new PageGridData<xjreviewprogressdto> { rows = data, status = 0, total = datacount, summary = new { pagecount = totalPages } };
        }

        public PageGridData<xjreviewprogressdto> GetDsjData(xjreviewprogressQuery options)
        {
            DynamicParameters sqlParams = new DynamicParameters();

            string sql = "   select  row_number() OVER () AS idx,  * from ( select  * from dsj_review_progress  where 1=1 order by citydm,sh_time asc  ) t where 1=1  ";
            string sqlcount = "    select  count(1)  from dsj_review_progress where 1=1 ";

            if (options.sq_sj != null && options.sq_sj.Count == 2)
            {
                var start_time = Convert.ToDateTime(options.sq_sj[0] + "").AddDays(1).ToString("yyyy/MM/dd") + "";
                var end_time = Convert.ToDateTime(options.sq_sj[1] + "").AddDays(1).ToString("yyyy/MM/dd") + "";
                //sj = sj.AddMinutes(59);
                //sj = sj.AddSeconds(59);
                sqlcount += " and a.create_time between '" + start_time + " 00:00:00' and '" + end_time + "  23:59:59'  ";
                sql += " and a.create_time between '" + start_time + " 00:00:00' and '" + end_time + " 23:59:59'  ";

            }
            if (options.sh_sj != null && options.sh_sj.Count == 2)
            {
                var start_time = Convert.ToDateTime(options.sh_sj[0] + "").AddDays(1).ToString("yyyy/MM/dd") + "";
                var end_time = Convert.ToDateTime(options.sh_sj[1] + "").AddDays(1).ToString("yyyy/MM/dd") + "";
                sqlcount += " and sh_time  between '" + start_time + " 00:00:00' and '" + end_time + "  23:59:59'  ";
                sql += " and sh_time  between '" + start_time + " 00:00:00' and '" + end_time + "  23:59:59'  ";
            }
            if (options.shzt != null)
            {
                if (options.shzt == -1)
                {
                    sqlcount += " and shzt ='未审核'  ";
                    sql += " and shzt ='未审核'   ";
                }
                else if (options.shzt == 1)
                {
                    sqlcount += " and shzt ='已通过'  ";
                    sql += " and shzt ='已通过'   ";
                }
                else if (options.shzt == 0)
                {
                    sqlcount += " and shzt ='不通过'  ";
                    sql += " and shzt ='不通过'   ";
                }
                else if (options.shzt == -3)
                {
                    sqlcount += " and shzt ='未提交'  ";
                    sql += " and shzt ='未提交'   ";
                }
            }
            if (!string.IsNullOrEmpty(options.citydm))
            {

                sqlcount += " and citydm='" + options.citydm + "'";
                sql += " and citydm='" + options.citydm + "'";
            }
            sql += "  limit @pagesize offset (@pageindex-1)*@pagesize  ";
            sqlParams.Add("pageindex", options.pageindex);
            sqlParams.Add("pagesize", options.pagesize);
            var datacount = repository.DapperContext.ExecuteScalar(sqlcount, sqlParams).GetInt();
            var data = repository.DapperContext.QueryList<xjreviewprogressdto>(sql, sqlParams).ToList();
            int totalPages = datacount / options.pagesize;
            if (datacount % options.pagesize != 0)
            {
                totalPages++;
            }
            return new PageGridData<xjreviewprogressdto> { rows = data, status = 0, total = datacount, summary = new { pagecount = totalPages } };
        }

    }
}
